package com.dave.admin.modular.boss.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dave.admin.common.result.CommonPage;
import com.dave.admin.common.result.CommonResult;
import com.dave.admin.component.log.SysLog;
import com.dave.admin.modular.boss.dto.WebLogParam;
import com.dave.admin.modular.boss.entity.WebLog;
import com.dave.admin.modular.boss.service.IWebLogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * BOSS日志 前端控制器
 * </p>
 *
 * @author Dave

 */
@Slf4j
@RestController
@RequestMapping("/boss/web-log")
@AllArgsConstructor
public class WebLogController extends BaseController {

    private final IWebLogService iWebLogService;

    @Override
    public void setPermissionPrefix() {
        super.permissionPrefix = "WebLog";
    }

    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param webLogParam 请求参数
     * @return Results 响应数据
     */
    @GetMapping("/page")
    @PreAuthorize("hasPermission(#this.getThis(),'')")
    public CommonResult page(Page page, WebLogParam webLogParam) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc(WebLog.CREATE_TIME);
        queryWrapper.between(
                Objects.nonNull(webLogParam.getStartTime()) && Objects.nonNull(webLogParam.getEndTime())
                , WebLog.CREATE_TIME
                , webLogParam.getStartTime()
                , webLogParam.getEndTime());

        IPage iPage = iWebLogService.page(page, queryWrapper);
        return CommonResult.success(CommonPage.restPage(iPage));
    }


    /**
     * 通过id查询
     *
     * @param id id
     * @return Results 响应数据
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasPermission(#this.getThis(),'-Detail')")
    public CommonResult getById(@PathVariable("id") String id) {
        return CommonResult.success(iWebLogService.getById(id));
    }


    /**
     * @param webLog 入参
     * @return Results 响应数据
     */
    @SysLog("添加操作日志")
    @PostMapping
    @PreAuthorize("hasPermission(#this.getThis(),'-Add')")
    public CommonResult save(@RequestBody WebLog webLog) {
        iWebLogService.save(webLog);
        return CommonResult.success();
    }


    /**
     * @param webLog 入参
     * @return Results 响应数据
     */
    @SysLog("更新操作日志")
    @PutMapping
    @PreAuthorize("hasPermission(#this.getThis(),'-Update')")
    public CommonResult updateById(@RequestBody WebLog webLog) {
        return CommonResult.success(iWebLogService.updateById(webLog));
    }


    /**
     * 通过id删除
     *
     * @param id id
     * @return Results 响应数据
     */
    @SysLog("通过ID删除操作日志")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasPermission(#this.getThis(),'-Delete')")
    public CommonResult removeById(@PathVariable String id) {
        return CommonResult.success(iWebLogService.removeById(id));
    }
}
